<?php
namespace app\client\controller;
use wechat\Wechat;
use org\MobileDetect;

class ShareController extends \think\Controller {
  /**
   *  @brief  : 闲聊客户端内嵌加入房间页面
   *  @return : page
   *  @details:
$game = '诸暨麻将';
$room = 229554;
$rule = '正1缺3,4局,AA支付,2-5-15,3个财吊,承包5倍,庄家必炮,无限制.';
$icon = 'http://wxsharefile.sz.p10.game918.net/niuniu/res/shareIcon.png';
$durl = 'http://down.cw.szmj.pay57.com/?from=groupmessage&isappinstalled=0';
http://127.0.0.1:882/client/share.html?game=%E8%AF%B8%E6%9A%A8%E9%BA%BB%E5%B0%86&room=229554&rule=%E6%AD%A31%E7%BC%BA3%2C4%E5%B1%80%2CAA%E6%94%AF%E4%BB%98%2C2-5-15%2C3%E4%B8%AA%E8%B4%A2%E5%90%8A%2C%E6%89%BF%E5%8C%855%E5%80%8D%2C%E5%BA%84%E5%AE%B6%E5%BF%85%E7%82%AE%2C%E6%97%A0%E9%99%90%E5%88%B6.&icon=http%3A%2F%2Fwxsharefile.sz.p10.game918.net%2Fniuniu%2Fres%2FshareIcon.png&durl=http%3A%2F%2Fdown.cw.szmj.pay57.com%2F%3Ffrom%3Dgroupmessage%26isappinstalled%3D0
   */
  public function indexAction() {
    $game = $this->request->param('game/s', '', 'trim');  // 游戏名
    $room = $this->request->param('room/s', 0); // 房间号
    $rule = $this->request->param('rule/s', '', 'trim');  // 规则描述
    $icon = $this->request->param('icon/s', '', 'trim');  // 游戏图标URL
    $durl = $this->request->param('durl/s', '', 'trim');  // 游戏下载页面URL
    $schm = $this->request->param('schm/s', '', 'trim');  // 业务参数

    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'game'  => $game,
      'room'  => $room,
      'rule'  => $rule,
      'icon'  => $icon,
      'durl'  => $durl,
      'schm'  => $schm,
    ]);
  }
  /**
   *  @brief  : 微信群分享链接, 包含`打开游戏&在线体验&下载游戏`等交互
   *  @return : page
   *  @details: http://cl.123pk123.com/share/game.html?query=
   */
  public function gameAction() {
    $ShareID = 43391;
    $share = config("share.{$ShareID}");
    if (empty($share)) {
      $ShareID = 1001; // 默认嵊州麻将
      $share = config("share.{$ShareID}");
    }
    $wxgzh = config("wxgzh.{$share['gzh_key']}");
    $query    = $this->request->param('query/s', '', 'trim,urlencode');  // APP启动参数
    $detect   = new MobileDetect;
    $isWeiXin = $detect->version('MicroMessenger');

    return $this->fetch('', [
      'share' => $share,
      'query' => $query,
      'wxgzh' => $wxgzh,
      'ShareID'   => $ShareID,
      'isWeiXin'  => $isWeiXin,
    ]);
  }
  /**
   *  @brief  : 动作重定向
   *  @return : think\response\RedirectResponse
   *  @details:
   */
  public function redirectAction() {
    
    $ShareID = $this->request->param('share/d', 0);
    $share = config("share.{$ShareID}");
    if (empty($share)) {
      $ShareID = 1001; // 默认嵊州麻将
      $share = config("share.{$ShareID}");
    }
    $type = $this->request->param('type/d', 3); // 动作类型 1=打开游戏; 2=在线体验; 3=下载游戏;
    $type = in_array($type, [1, 2, 3]) ? $type : 3;
    $host = strtolower($this->request->host(true));
    $query = $this->request->param('query/s', '', 'trim,urlencode');  // APP启动参数
    $conf = config("wxgzh.{$share['gzh_key']}");

    $detect     = new MobileDetect;
    $isMobile   = $detect->isMobile();  // 是否为移动端, 包括手机和平板
    $isTablet   = $detect->isTablet();  // 是否为平板
    $isIOS      = $detect->isIOS();
    $isAndroid  = $detect->isAndroidOS();
    $isWeiXin   = $detect->version('MicroMessenger');

    if (!$isMobile) {
      return response('', 405);
    }
    if ($type == 1) { // 打开游戏
      $app_scheme = $share['app_scheme'];  // APP启动协议
      $app_host   = $share['app_host'];
      if (empty($app_scheme)) {
        return response('', 500); // 无法完成请求
      } else {
        return $this->redirect("{$app_scheme}://{$app_host}?{$query}"); // 完整
      }
    } else if ($type == 2) {  // 在线体验
      $ref = url('authorize', '', true, true)."?share={$ShareID}&query={$query}";
      $ref = urlencode($ref);
      $url = url('base/authorize', '', true, $conf['BindHost']);
      $url = "{$url}?ref={$ref}&state=AGENT0SHAREID{$ShareID}";
      $wx = new Wechat($conf);
      $eax = $wx->getOauthRedirect($url, '', Wechat::SNSAPI_USERINFO);
      return $this->redirect($eax);
    } else if ($type == 3) {  // 下载游戏
      $url = url('/download', '', true, $conf['BindHost']);
      $url = $url."?state=AGENT0SHAREID{$ShareID}";
      return $this->redirect($url);
    }
    return response('', 500);  // 无法完成请求
  }
  /**
   *  @brief  : 公众号授权回调&重定向到H5页面
   *  @return : RedirectResponse
   *  @details:
   */
  public function authorizeAction() {
    $ShareID = $this->request->param('share/d', 0); // 游戏分享码
    $share = config("share.{$ShareID}");
    if (empty($share)) {
      $ShareID = 1001; // 默认嵊州麻将
      $share = config("share.{$ShareID}");
    }
    $code = $this->request->param('code/s', '', 'trim'); // code
    $query = $this->request->param('query/s', '', 'trim,urlencode'); // APP启动参数
    return $this->redirect("{$share['app_online']}?code={$code}&query={$query}&channel={$share['channel']}");
  }
}
